// wx2db0b20b87789f11

const path = require('path');
const ci = require('miniprogram-ci');
const inquirer = require('inquirer');
const projectConfig = require('../project.config.json');
const root = path.resolve(__dirname, '..');
let projectPath =
  process.env.NODE_ENV === 'production'
    ? path.join(root, 'build/dev/mp-weixin/')
    : path.join(root, 'dist/dev/mp-weixin/');
// new ci实例
const project = new ci.Project({
  appid: projectConfig.appid,
  type: 'miniProgram',
  projectPath: projectPath,
  privateKeyPath: path.join(root, 'ci/private.key'),
  ignores: ['node_modules/**/*']
});

/** 上传 */
async function upload({ version = '1.0.0', desc = 'test' }) {
  await ci.upload({
    project,
    version,
    desc,
    setting: {},
    onProgressUpdate: console.log
  });
}

function inquirerResult() {
  return inquirer.prompt([
    {
      type: 'input',
      name: 'version',
      message: `请输入上传的版本号:`,
      validate(v) {
        if (!v) return new Error('请输入上传的版本号');
        return true;
      }
    },

    {
      type: 'input',
      name: 'desc',
      message: `请描述这个版本的改动:`,
      validate(v) {
        if (!v) return new Error('请描述这个版本的改动');
        return true;
      }
    }
  ]);
}

async function init() {
  let result = await inquirerResult();
  upload(result);
}

init();
